package com.sky.robot.frame.controller;

import com.sky.robot.commons.consts.GeneratorConst;
import com.sky.robot.commons.utils.PropertiesUtils;
import com.sky.robot.commons.utils.StringUtil;
import com.sky.robot.frame.entity.TableEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BuildTableController {

    private static final String SQL_SHOW_TABLES = "show table status";

    private static Logger logger = LoggerFactory.getLogger(BuildTableController.class);

    private static Connection connection = null;

    static {
        String driverClassName = PropertiesUtils.getString("spring.dataSource.driver-class-name" );
        String url = PropertiesUtils.getString("spring.dataSource.url" );
        String username = PropertiesUtils.getString("spring.dataSource.username" );
        String password = PropertiesUtils.getString("spring.dataSource.password" );

        try {
            Class.forName(driverClassName);
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("连接成功");
        } catch (Exception e) {
            e.printStackTrace();
//            logger.info("连接数据库失败" );
        }
    }

    public static void getTables() {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        List<TableEntity> tableEntities = new ArrayList<>();


        try {
            preparedStatement = connection.prepareStatement(SQL_SHOW_TABLES);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String tableName = resultSet.getString("name" );

                TableEntity tableEntity = new TableEntity();
                tableEntity.setTableName(tableName);

                String beanName = tableName;

                if (GeneratorConst.IGNORE_TABLE_PREFIX) {
                    beanName = StringUtil.caseFirstLetter(tableName, true);
                }

                tableEntity.setBeanName(beanName);
                System.out.println(tableEntity);
            }
        } catch (Exception e) {
//            logger.info("读取表数据失败" );
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (Exception e) {
//                    logger.info(e.getMessage());
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (Exception e) {
//                    logger.info(e.getMessage());
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
//                    logger.info(e.getMessage());
                }
            }

        }
    }

    public static void main(String[] args) {
        getTables();
    }
}
